package com.housekeeping.housekeeping.adManager.controller;


import com.alibaba.fastjson.JSONObject;
import com.housekeeping.housekeeping.adManager.entity.Category;
import com.housekeeping.housekeeping.adManager.service.CategoryService;
import com.housekeeping.housekeeping.adManager.service.impl.CategoryServiceImpl;
import com.housekeeping.housekeeping.adManager.utils.ResponseEntity;
import com.housekeeping.housekeeping.adManager.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 添加分类信息
 */
@WebServlet("/category/add")
public class AddCateController extends HttpServlet {
    private CategoryService service = new CategoryServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryJson = req.getParameter("category");
        Category category = JSONObject.parseObject(categoryJson,Category.class);
        ResponseEntity<Object> entity = null;
        try {
            int i = service.saveCategory(category);
            if (i > 0){
                entity = ResponseEntity.success(null);
            } else {
                entity = ResponseEntity.error(400005,"添加失败");
            }
        } catch (SQLException e) {
            entity = ResponseEntity.error(400006,"网络异常");
        }
        ResponseUtils.resp(resp,entity);
    }
}
